package benny_driving_ser.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.dao.impl.AppVersionDaoImpl;
import benny_driving_ser.entity.AppVersion;

public class VersionUpdataServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7939975948359042239L;

	/**
	 * Constructor of the object.
	 */
	public VersionUpdataServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String action = "";
		if (null == (action = request.getParameter("action").trim())){
			out.close();
			return;
		}
		if ("dri-version".equals(action)) {
			AppVersion av = AppVersionDaoImpl.getInstance().getDriVersion();
			JSONObject j = new JSONObject();
			try {
				j.put("url", av.getUrl());
				j.put("version", av.getVersion());
				out.print(j.toString());
			} catch (JSONException e) {
				e.printStackTrace();
				out.print("{'url':'null','version':'null'}");
			}
		}
		
		if ("user-version".equals(action)) {
			AppVersion av = AppVersionDaoImpl.getInstance().getUserVersion();
			JSONObject j = new JSONObject();
			try {
				j.put("url", av.getUrl());
				j.put("version", av.getVersion());
				out.print(j.toString());
			} catch (JSONException e) {
				e.printStackTrace();
				out.print("{'url':'null','version':'null'}");
			}
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
